package com.budwk.app.sqfw.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.budwk.app.sqfw.constants.SqfwVerifyCodeTypeConstants;
import com.budwk.app.sqfw.models.Sqfw_verifyCode;
import com.budwk.app.sqfw.services.SqfwVerifyCodeService;
import com.budwk.app.uc.services.ValidateService;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

/**
 * @author ZY.Wang
 * @since 2023/7/10
 */
@IocBean
@At("/platform/sqfw/verifyCode")
@ApiDefinition(tag = "报告文件")
@Slf4j
public class SqfwVerifyCodeController {

    @Inject
    private SqfwVerifyCodeService sqfwVerifyCodeService;
    @Inject
    private ValidateService validateService;

    @At("/sendCodeForLogin")
    @Ok("json")
    @POST
    @ApiOperation(name = "短信登陆发送验证码")
    @SLog(tag = "短信登陆发送验证码", value = "${args[0]},${args[1]},${args[2]}")
    public Result<?> sendCodeForLogin(@Param("phoneNo") String phoneNo,
                                      @Param("captchaKey") String captchaKey,
                                      @Param("captchaCode") String captchaCode) {
        // 如果后续需要校验图形验证码
        if (StringUtils.isNotBlank(captchaKey) || StringUtils.isNotBlank(captchaCode))
            validateService.checkCode(captchaKey, captchaCode);
        int sendCount = sqfwVerifyCodeService.commonSendCode(phoneNo, SqfwVerifyCodeTypeConstants.login_verify_code);
        if (sendCount <= 0) return Result.error();
        return Result.success(sendCount);
    }

    @At("/sendCodeForRestPassword")
    @Ok("json")
    @POST
    @ApiOperation(name = "忘记密码发送验证码")
    @SLog(tag = "忘记密码发送验证码", value = "${args[0]}")
    public Result<?> sendCodeForRestPassword(@Param("phoneNo") String phoneNo) {
        int sendCount = sqfwVerifyCodeService.commonSendCode(phoneNo, SqfwVerifyCodeTypeConstants.rest_password);
        if (sendCount <= 0) return Result.error();
        return Result.success(sendCount);
    }


    @At("/checkCodeForRestPassword")
    @Ok("json")
    @POST
    @ApiOperation(name = "忘记密码验证验证码")
    @SLog(tag = "忘记密码验证验证码", value = "${args[0]},${args[1]}")
    public Result<?> checkCodeForRestPassword(@Param("phoneNo") String phoneNo, @Param("verifyCode") String verifyCode) {
        Sqfw_verifyCode checkCode = sqfwVerifyCodeService.checkCode(phoneNo, verifyCode, SqfwVerifyCodeTypeConstants.rest_password);
        if (checkCode == null) return Result.error();
        return Result.success();
    }

    @At("/sendCodeForVerifyPeerPerson")
    @Ok("json")
    @POST
    @ApiOperation(name = "同行人员验证码")
    @SaCheckLogin
    @SLog(tag = "同行人员验证码", value = "${args[0]}")
    public Result<?> sendCodeForVerifyPeerPerson(@Param("phoneNo") String phoneNo) {
        int sendCode = sqfwVerifyCodeService.commonSendCode(phoneNo, SqfwVerifyCodeTypeConstants.verify_peer_person);
        if (sendCode == 0) return Result.error("发送失败");
        if (sendCode == -1) return Result.error("发送频繁，请稍后再试");
        return Result.success(sendCode);
    }

    @At("/checkCodeForVerifyPeerPerson")
    @Ok("json")
    @POST
    @ApiOperation(name = "同行人员验证验证码")
    @SaCheckLogin
    @SLog(tag = "同行人员验证验证码", value = "${args[0]},${args[1]}")
    public Result<?> checkCodeForVerifyPeerPerson(@Param("phoneNo") String phoneNo, @Param("verifyCode") String verifyCode) {
        Sqfw_verifyCode checkCode = sqfwVerifyCodeService.checkCode(phoneNo, verifyCode, SqfwVerifyCodeTypeConstants.verify_peer_person);
        if (checkCode == null) return Result.error("验证码输入错误");
        return Result.success();
    }
}
